<template>
  <div class="login-wrapper">
    <div class="login-main">
        <h3 class="login-title">后台管理系统模板</h3>
    <el-form
      ref="loginForm"
      :model="loginForm"
      class="loginContainer"
      :rules="rules"
      v-loading="loading"
      element-loading-text="正在登录"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
    >
      <el-form-item prop="username">
        <el-input
          type="text"
          v-model="loginForm.username"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          type="password"
          v-model="loginForm.password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>

      <el-form-item prop="code" class="codeclass">
          <el-input
          type="text"
          v-model="loginForm.code"
          placeholder="点击图片更换验证码"
          style="width: 200px; margin-right: 5px;"
        ></el-input>
       <img :src="captchaUrl" alt="" @click="updateCaptcha()" />
      </el-form-item> 
      <el-checkbox v-model="checked" class="loginRemember">记住我</el-checkbox>
      <el-button
        type="primary"
        style="width: 100%"
        @click="subLogin('loginForm')"
        class="login-btn"
        >登录</el-button
      >
    </el-form>
    </div>
     <canvas id="canvas" class="login-anim"></canvas>
  </div>
</template>
<script>
export default {          
  name: "Login",
  data() {
    return {
      captchaUrl: "/captcha?time=" + new Date(),
      loginForm: {
        username: "",
        password: "",
        code: "",
      },
      loading: false,
      checked: true,
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        code: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      },
    };
  },
  methods: {
    updateCaptcha() {
       this.captchaUrl = "/captcha?time=" + new Date()
        layui.use(["layer", "form"], function () {
          var layer = layui.layer,
            form = layui.form;
           layer.msg("Hello World");
        });  
    },
    subLogin(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          new Promise((resolve, reject) => {
            resolve(this.postRequest("/login", this.loginForm));
          }).then((resp) => {
            if (resp) {
              this.loading = false;
              // 存储用户token
              const tokenStr = resp.obj.tokenHead + " " + resp.obj.token;
              window.sessionStorage.setItem("tokenStr", tokenStr);
              if (!window.sessionStorage.getItem("user")) {
                //session 中 user 为空， 发起请求 将user 放入session
                return new Promise((resolve, reject) => {
                  resolve(this.getRequest("admin/info"));
                }).then((resp) => {
                  if (resp) {
                    this.$store.commit("initUser", resp);
                    //跳转首页
                    let path = this.$route.query.redirect;
                    this.$router.replace(
                      path == "/" || path == undefined ? "/Home" : path
                    );
                  }
                });
              }
            }
          });
        } else {
          this.$message.error("请输入所有字段");
          return false;
        }
      });
      this.loading = false;
    },
  },
  mounted() {
      // 实现页面底部波纹特效
      let canvas = document.getElementById('canvas');
      let ctx = canvas.getContext('2d');
      canvas.width = canvas.parentNode.offsetWidth;
      canvas.height = canvas.parentNode.offsetHeight;
      console.log(canvas.width, canvas.height);
      // 如果浏览器支持requestAnimFrame则使用requestAnimFrame否则使用setTimeout
      window.requestAnimFrame = (function () {
        return window.requestAnimationFrame
          || window.webkitRequestAnimationFrame
          || window.mozRequestAnimationFrame
          || function (callback) {
            window.setTimeout(callback, 1000 / 60);
          };
      })();

      // 波浪大小
      let boHeight = 40;// canvas.height / 10;
      let posHeight = canvas.height - 150;// canvas.height / 1.2;

      // 初始角度为0
      let step = 0;
      // 定义三条不同波浪的颜色
      let lines = ["rgba(69, 159, 117, 0.1)", "rgba(95, 170, 135, 0.6)", "rgba(69, 159, 117, 0.4)"];

      function loop(){
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        step++;
        // 画3个不同颜色的矩形
        for(let j = lines.length - 1; j >= 0; j--) {

          ctx.fillStyle = lines[j];

          //每个矩形的角度都不同，每个之间相差45度
          let angle = (step + j * 70) * Math.PI / 180; // 50
          let deltaHeight = Math.sin(angle) * boHeight;
          let deltaHeightRight = Math.cos(angle) * boHeight;
          ctx.beginPath();
          ctx.moveTo(0, posHeight + deltaHeight);
          ctx.moveTo(0, posHeight + deltaHeight);
          ctx.bezierCurveTo(canvas.width / 2, posHeight + deltaHeight-boHeight, canvas.width / 2, posHeight + deltaHeightRight - boHeight, canvas.width, posHeight + deltaHeightRight);
          ctx.lineTo(canvas.width, canvas.height);
          ctx.lineTo(0, canvas.height);
          ctx.lineTo(0, posHeight+deltaHeight);
          ctx.closePath();
          ctx.fill();
        }
        requestAnimFrame(loop);
      }
      loop();
    },
};
</script>
<style scoped lang="less">
.login-wrapper {
    height: 100%;
  }
  #canvas {
    width: 100%;
  }
  .login-main {
    position: absolute;
    background-clip: padding-box;
    top: 50%;
    left: 50%;
    width: 380px;
    height: 420px;
    padding: 20px 35px;
    border: 1px solid #eee;
    margin: -185px 0 0 -160px;
    box-shadow: 0 0 25px #cac6c6;
    &::before, &::after {
      position: absolute;
      height: 12px;
      content: "";
    }
    &::before {
      left: 4px;
      right: 4px;
      top: -12px;
      z-index: 2;
      background-color: #f5f5f5;
    }
    &::after {
      left: 10px;
      right: 10px;
      top: -22px;
      z-index: 1;
      background-color: #f0f0f0;
    }
  }
  .codeclass {
     /deep/ .el-form-item__content{
        display: flex;
        align-items: center;
   }
  }
  .login-title {
    padding-bottom: 15px;
    border-bottom: 2px solid @mainColor;
    margin: 15px 0 45px 0;
    color: #555;
    text-align: center;
    font-size: 30px;
  }
  .login-btn{
    width: 100%;
    height: 36px;
    margin-top: 30px;
    font-size: 16px;
  }
  .login-anim {
    position: absolute;
    bottom: 0;
    left: 0;
    z-index: -1;
  }
</style>